#ifndef _DSWCONSOLE_RF_H
#define _DSWCONSOLE_RF_H

#include "dswconsole_base.h"
#include "rf_def.h"

#define RF_COMM_HEAD "\xc0\x53\x06"
#define RF_COMM_HEAD_SIZE 3
#define RF_COMM_PAYLOADSIZE 6

#define RF_RECV_TIMEOFFSET 1

#define ISVALIDRFID(x) (((x & 0xff000000) >= REMOTEID) && ((x & 0xff000000) <= CURTAINID))

typedef std::list<ST_RFRecv*> LIST_SEND_T, *LPLIST_SEND_T;

class CDswconsole_rf : public CDswconsole_base
{
public:
	CDswconsole_rf(LPST_ENV_T pEnv, EN_CON_SESSION_TYPE nType, LPST_CONSOLE_INFO_T pInfo);
	virtual ~CDswconsole_rf();
public:
	static CDswconsole_rf* createNew(LPST_ENV_T pEnv);

	static int cbOnHanderRemote(ST_RFRecv * pRecv_tlv, int ncmd_rsp, ST_RFRecv * pResult_tlv, void * pCaller){return ((CDswconsole_rf*)pCaller)->OnHanderRemote(pRecv_tlv, ncmd_rsp, pResult_tlv);};
	static int cbOnHanderEmerg(ST_RFRecv * pRecv_tlv, int ncmd_rsp, ST_RFRecv * pResult_tlv, void * pCaller){return ((CDswconsole_rf*)pCaller)->OnHanderEmerg(pRecv_tlv, ncmd_rsp, pResult_tlv);};
	static int cbOnHanderDoor(ST_RFRecv * pRecv_tlv, int ncmd_rsp, ST_RFRecv * pResult_tlv, void * pCaller){return ((CDswconsole_rf*)pCaller)->OnHanderDoor(pRecv_tlv, ncmd_rsp, pResult_tlv);};
	static int cbOnHanderSmoke(ST_RFRecv * pRecv_tlv, int ncmd_rsp, ST_RFRecv * pResult_tlv, void * pCaller){return ((CDswconsole_rf*)pCaller)->OnHanderSmoke(pRecv_tlv, ncmd_rsp, pResult_tlv);};
	static int cbOnHandervirberion(ST_RFRecv * pRecv_tlv, int ncmd_rsp, ST_RFRecv * pResult_tlv, void * pCaller){return ((CDswconsole_rf*)pCaller)->OnHandervirberion(pRecv_tlv, ncmd_rsp, pResult_tlv);};
	static int cbOnHanderCurtain(ST_RFRecv * pRecv_tlv, int ncmd_rsp, ST_RFRecv * pResult_tlv, void * pCaller){return ((CDswconsole_rf*)pCaller)->OnHanderCurtain(pRecv_tlv, ncmd_rsp, pResult_tlv);};

	static void cbBroadcastSetGarrison(int nCmd, void* pParam, void* pCaller){((CDswconsole_rf*)pCaller)->BroadcastSetGarrison(nCmd, pParam);};
	static void cbBroadcastSetAreAlarm(int nCmd, void* pParam, void* pCaller){((CDswconsole_rf*)pCaller)->BroadcastSetAreAlarm(nCmd, pParam);};
private:
	virtual bool SeekToStartComm(LPST_COMM_RECV_T pCommRecv, int &nPayloadSize);
	virtual void ProcessData(char* pData, int nLen);

	void SendData(LPST_RFRecv pTlv){evbuffer_add(bufferevent_get_output(m_pBev), pTlv, 3 + pTlv->elme3);};
	bool IsInSavedRFID(LPST_RFRecv pTlv, LPST_ARE_PARAM pSaved);

	int LearnRFID(LPST_RFRecv pTlv, LPST_ARE_PARAM pSaved);
	
	int OnHanderRemote(ST_RFRecv * pRecv_tlv, int ncmd_rsp, ST_RFRecv * pResult_tlv);
	void DealHandOn(ST_RFRecv * pTlv);
	void DealHandOff(ST_RFRecv * pTlv);
	void DealHandMut(ST_RFRecv * pTlv);
	void DealHandSOS(ST_RFRecv * pTlv);
	
	int OnHanderEmerg(ST_RFRecv * pRecv_tlv, int ncmd_rsp, ST_RFRecv * pResult_tlv);
	int OnHanderDoor(ST_RFRecv * pRecv_tlv, int ncmd_rsp, ST_RFRecv * pResult_tlv);
	int OnHanderSmoke(ST_RFRecv * pRecv_tlv, int ncmd_rsp, ST_RFRecv * pResult_tlv);
	int OnHandervirberion(ST_RFRecv * pRecv_tlv, int ncmd_rsp, ST_RFRecv * pResult_tlv);
	int OnHanderCurtain(ST_RFRecv * pRecv_tlv, int ncmd_rsp, ST_RFRecv * pResult_tlv);

	void BroadcastSetGarrison(int nCmd, void* pParam);
	void BroadcastSetAreAlarm(int nCmd, void* pParam);
private:
	static void cbHeartBeet(int fd, short events, void* ctx){((CDswconsole_rf*)ctx)->HeartBeet(fd, events);};
	void HeartBeet(int fd, short events);
	static void cbSendTask(int fd, short events, void* ctx){((CDswconsole_rf*)ctx)->SendTask(fd, events);};
	void SendTask(int fd, short events);
	void SetSoundLight(bool bOn);
	void SetTrigSoundLight();
	void SetPoliceLock(bool bOn);
public:
	void BroadcastConsoleCmd(void* pParam);
private:
	timer_param_t m_nTimer, m_nSendTask;
	unsigned m_nLastRfid;
	time_t m_nLastRecvTime;
	LIST_SEND_T m_nSendList;
	pthread_mutex_t m_nLock;
};

#endif

